package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.debug.ServletDebugInfo;
import com.conversationboard.formatter.AutoLinkFormatter;
import com.conversationboard.formatter.Formatter;
import com.conversationboard.formatter.IllegalMessageException;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.User;
import com.conversationboard.model.UserProfile;
import com.conversationboard.security.EncryptedKey;
import com.conversationboard.security.EncryptionException;

@WebServlet(name = "EditProfileControllerServlet", urlPatterns = "/EditProfileControllerServlet")
public class EditProfileControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -8864568778269659449L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {

			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			String key = request.getParameter("key");
			EncryptedKey encryptedKey = new EncryptedKey();

			if (!encryptedKey.isValid(key, user.getLoginId())) {
				Logger.log(new Date(), "Part 1 of error: " + this.getClass().getName(), user);
				String postRequest = ServletDebugInfo.getRawRequest(request);
				Logger.log(new Date(), "Part 2 of error:\n\n" + postRequest, user);
				return;
			}

			String name = request.getParameter("name").trim();
			String email = request.getParameter("email").trim();
			String location = request.getParameter("location");
			String websiteUrl = request.getParameter("websiteurl").trim();
			String info = request.getParameter("info").trim();

			/* Trim the data if it's too long */

			if (name.length() >= 200) {
				name = name.substring(0, 199);
			}

			if (email.length() >= 200) {
				email = email.substring(0, 199);
			}

			if (location.length() >= 200) {
				location = location.substring(0, 199);
			}

			if (websiteUrl.length() >= 200) {
				websiteUrl = websiteUrl.substring(0, 199);
			}

			if (info.length() >= 2000) {
				info = info.substring(0, 1999);
			}

			UserProfile userProfile = user.getUserProfile();

			String url = Formatter.escapeHtml(websiteUrl);
			url = AutoLinkFormatter.format(url, null, user);

			userProfile.setFullName(Formatter.escapeHtml(name));
			userProfile.setLocation(Formatter.escapeHtml(location));
			userProfile.setInfo(Formatter.escapeHtml(info));
			userProfile.setEmail(Formatter.escapeHtml(email));
			userProfile.setWebsiteUrl(url);

			user.storeUserProfile();

			RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
			dispatcher.forward(request, response);

		} catch (IllegalMessageException e) {
			throw new ServletException(e);
		} catch (EncryptionException e) {
			throw new ServletException(e);
		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
